/**
 *    Copyright 2009-2017 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package org.apache.ibatis.mapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.property.PropertyTokenizer;
import org.apache.ibatis.session.Configuration;

/**绑定的SQL，是从SqlSource而来，将动态内容都处理完成得到的SQL语句字符串，其中包括?，还有绑定的参数，是对Executor组件执行sql信息的封装
 * </br>An actual SQL String got from an {@link SqlSource} after having processed any
 * dynamic content. The SQL may have SQL placeholders "?" and an list (ordered)
 * of an parameter mappings with the additional information for each parameter
 * (at least the property name of the input object to read the value from).
 * </br>Can also have additional parameters that are created by the dynamic language (for loops, bind...).
 * 
 * @author Clinton Begin
 */
public class BoundSql {

	/**Mapper配置解析后的sql语句
	 * 
	 */
	private final String sql;
	/**Mapper参数映射信息
	 * 
	 */
	private final List<ParameterMapping> parameterMappings;
	/**Mapper参数对象
	 * 
	 */
	private final Object parameterObject;
	/**额外参数信息，包括<bind>标签绑定的参数，内置参数。Mybatis任意一个Mapper都有两个内置参数，即_parameter、_databaseId。
	 * <br/>_parameter代表整个参数，包括<bind>标签绑定的参数信息，这些参数存放在additionalParameters属性中；
	 * <br/>_databaseId为Mapper配置中通过databaseId属性指定的数据库类型
	 * 
	 */
	private final Map<String, Object> additionalParameters;
	/**参数对象对应的MetaObject对象
	 * 
	 */
	private final MetaObject metaParameters;

	public BoundSql(Configuration configuration, String sql, List<ParameterMapping> parameterMappings, Object parameterObject) {
		this.sql = sql;
		this.parameterMappings = parameterMappings;
		this.parameterObject = parameterObject;
		this.additionalParameters = new HashMap<String, Object>();
		this.metaParameters = configuration.newMetaObject(additionalParameters);
	}

	public String getSql() {
		return sql;
	}

	public List<ParameterMapping> getParameterMappings() {
		return parameterMappings;
	}

	public Object getParameterObject() {
		return parameterObject;
	}

	public boolean hasAdditionalParameter(String name) {
		String paramName = new PropertyTokenizer(name).getName();
		return additionalParameters.containsKey(paramName);
	}

	public void setAdditionalParameter(String name, Object value) {
		metaParameters.setValue(name, value);
	}

	public Object getAdditionalParameter(String name) {
		return metaParameters.getValue(name);
	}
}
